﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sort
{
    static class Utils
    {
        public static void Swap(int[] arr, int idx1, int idx2)
        {
            try
            {
                int temp = arr[idx1];
                arr[idx1] = arr[idx2];
                arr[idx2] = temp;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        public static void WriteArr(int[] arr)
        {
            foreach (int v in arr)
            {
                Console.Write(v + ", ");
            }
            Console.Write("\n");
        }

        public static bool CheckOrderArray(int[] arr)
        {
            int val = arr[0];
            for (int i = 1; i < arr.Length; i++)
            {
                if (arr[i] < val)
                {
                    return false;
                }
                else
                {
                    val = arr[i];
                }
            }
            return true;
        }

        public static void CheckRunTime(Action action, string str = "")
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();

            watch.Start();

            action();

            watch.Stop();

            TimeSpan timespan = watch.Elapsed;  //获取当前实例测量得出的总时间
            Console.WriteLine("\n---------------------");
            Console.WriteLine(str + "代码执行时间：{0}(毫秒)", timespan.TotalMilliseconds);  //总毫秒数
            Console.WriteLine("---------------------\n");
        }
    }
}
